﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using BasicForms;
using System.IO;
using System.Diagnostics;
using Lists;

namespace Portal
{
    public partial class FrmThongTinCoSoThuoc : FrmOKCancel
    {
        public FrmThongTinCoSoThuoc()
        {
            InitializeComponent();
        }

        private void FrmThongTinCoSoThuoc_Load(object sender, EventArgs e)
        {
            if (DesignMode) return;
            txtTenCoSo.Text = GeneralLibs.Config.TenDoanhNghiep;  
            txtChuCoSo.Text = GeneralLibs.Config.ChuCoSo;  
            txtSoChungChi.Text = GeneralLibs.Config.SoChungChi;  
            dateNgayCap.EditValue = GeneralLibs.Config.NgayCapChungChi;  
            txtDienThoai.Text = GeneralLibs.Config.DienThoai;  
            txtEmail.Text = GeneralLibs.Config.Email;  
            txtDiaChi.Text = GeneralLibs.Config.DiaChi;  
            txtPhamViHoatDong.Text = GeneralLibs.Config.PhamViHoatDong;

            Logo logo = DanhMucLogo.GetFirst();
            if (logo != null)
            {
                MemoryStream mem = new MemoryStream(logo.AnhLogo);
                pictureBoxLogo.Image = Image.FromStream(mem);
                pictureBoxLogo.SizeMode = PictureBoxSizeMode.StretchImage;
            }
        }

        protected override void btnOK_Click(object sender, EventArgs e)
        {
            GeneralLibs.Config.TenDoanhNghiep = txtTenCoSo.Text.Trim();
            GeneralLibs.Config.ChuCoSo = txtChuCoSo.Text.Trim();
            GeneralLibs.Config.SoChungChi = txtSoChungChi.Text.Trim();
            if (dateNgayCap.EditValue is DateTime)
                GeneralLibs.Config.NgayCapChungChi = (DateTime)dateNgayCap.EditValue;
            else
                GeneralLibs.Config.NgayCapChungChi = null;
            GeneralLibs.Config.DienThoai = txtDienThoai.Text.Trim();
            GeneralLibs.Config.Email = txtEmail.Text.Trim();
            GeneralLibs.Config.DiaChi = txtDiaChi.Text.Trim();
            GeneralLibs.Config.PhamViHoatDong = txtPhamViHoatDong.Text.Trim();
            Close();
        }

        private void btnLuuAnh_Click(object sender, EventArgs e)
        {
            string pathSource = "";
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() != DialogResult.Cancel)
            {
                CreateAttachment(openFileDialog.FileName);  //upload the attachment
            }
        }
        private void CreateAttachment(string strFile)
        {
            try
            {
                FileStream objFileStream = new FileStream(strFile, FileMode.Open, FileAccess.Read);
                int intLength = Convert.ToInt32(objFileStream.Length);
                byte[] objData;
                objData = new byte[intLength];
                DataRow objRow;
                string[] strPath = strFile.Split(Convert.ToChar(@"\"));

                objFileStream.Read(objData, 0, intLength);
                objFileStream.Close();

                Logo logo = DanhMucLogo.GetFirst();
                if (logo != null)
                {
                    logo.AnhLogo = objData;
                    logo.UpdateToDB();
                }
                else
                {
                    logo = DanhMucLogo.Instance.NewDoiTuong();
                    logo.AnhLogo = objData;
                    logo.InsertToDB();
                }

                //Xong thì hiện lên picture box
                MemoryStream mem = new MemoryStream(objData);
                pictureBoxLogo.Image = Image.FromStream(mem);
                pictureBoxLogo.SizeMode = PictureBoxSizeMode.StretchImage;
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void btnXoaLogo_Click(object sender, EventArgs e)
        {
            try
            {
                Logo logo = DanhMucLogo.GetFirst();
                if (logo != null)
                {
                    logo.DeleteFromDB();
                }

                //Xong thì hiện null lên picture box
                pictureBoxLogo.Image = null;
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }
        
    }
}

